home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / dtk_demo.zip / COMPARE.C < prev    next >
C/C++ Source or Header  |  1991-09-12  |  4KB  |  164 lines

  1. /*  COMPARE.C
  2.  *  multiple file comparison utility
  3.  *  last mod.: 1-AUG-91
  4.  */
  5.  
  6. #include <STDIO.H>
  7. #include <FCNTL.H>
  8. #include <SYS\TYPES.H>
  9. #include <SYS\STAT.H>
  10. #include <IO.H>
  11. #include <STDLIB.H>
  12. #include <STRING.H>
  13.  
  14. #include <L_FILE.H>
  15. #include <L_DIR.H>
  16.  
  17. #define MSC TRUE       /*  set to FALSE for Turbo C/C++  */
  18.  
  19. #if MSC
  20. #include <MALLOC.H>
  21. #endif
  22.  
  23. char *usage = "\nUse: COMPARE file_1 file_2"
  24.               "\n     wildcards OK (e.g. COMPARE *.TXT *.DEC)\n";
  25.  
  26. char path1[_MAX_PATH_];
  27. char path2[_MAX_PATH_];
  28. char drive1[_MAX_DRIVE_];
  29. char drive2[_MAX_DRIVE_];
  30. char dir1[_MAX_PATH_];
  31. char dir2[_MAX_PATH_];
  32. char file1[_MAX_FILE_];
  33. char file2[_MAX_FILE_];
  34.  
  35. Str_ptr buff1 = NULL;
  36. Str_ptr buff2 = NULL;
  37. long offset;
  38. Uint buffsize;
  39.  
  40. void main (int argc, char **argv);
  41. int compare_files(Str_ptr file1, Str_ptr file2);
  42.  
  43. /*---------------------------*/
  44. void main(int argc,char **argv)
  45. {
  46. int i, n, err_flag;
  47. File *files_1;
  48. File *files_2;
  49.  
  50. if ( argc < 3 )
  51.     {
  52.     printf(usage);
  53.     exit(0);
  54.     }
  55.  
  56. for ( i=0; i<argc; i++ )
  57.     strupr(argv[i]);
  58.  
  59. split_path(argv[1],drive1,dir1,NULL,NULL);
  60. split_path(argv[2],drive2,dir2,NULL,NULL);
  61.  
  62. n = get_input_output_files(argc,argv,&files_1,&files_2,NULL,&err_flag);
  63. /*  this function allocates 2*n*sizeof(File) bytes from
  64.  *  the near heap to hold the two File arrays
  65.  */
  66. if ( err_flag )
  67.     {
  68.     switch ( err_flag )
  69.         {
  70.         case -1: printf("Not enough command line parameters.\n"); break;
  71.         case -2: printf("Invalid command line parameters.\n"); break;
  72.         case -3: printf("Out of near heap space.\n"); break;
  73.         case -4: break;     /*  ignore this error condition  */
  74.         default: printf("Unknown error.\n");
  75.         }
  76.     }
  77. else if ( !n )
  78.     printf("No files found matching %s.\n",argv[1]);
  79. else
  80.     {
  81.     #if MSC
  82.     buffsize = _memmax();
  83.     #else
  84.     buffsize = coreleft();
  85.     #endif
  86.     buffsize = ( buffsize - 128 ) / 2;
  87.     if ( ( buff1 = malloc(buffsize) ) != NULL )
  88.         buff2 = malloc(buffsize);
  89.     if ( buff2 == NULL )
  90.         printf("Insufficient memory in near heap.\n");
  91.     else
  92.         {
  93.         printf("Files matching %s: %d\n",argv[1],n);
  94.         for ( i=0; i<n; i++ )
  95.             {
  96.             printf("%12s %12s  ",files_1[i].name,files_2[i].name);
  97.             make_path(path1,drive1,dir1,files_1[i].name);
  98.             make_path(path2,drive2,dir2,files_2[i].name);
  99.             switch ( compare_files(path1,path2) )
  100.                 {
  101.                 case  0: printf("Files are the same.\n"); break;
  102.                 case -1: printf("First file not found.\n"); break;
  103.                 case -2: printf("Second file not found.\n"); break;
  104.                 case -3: printf("Read error with first file.\n"); break;
  105.                 case -4: printf("Read error with second file.\n"); break;
  106.                 case -5: printf("Files have different sizes.\n"); break;
  107.                 case -6: printf("Files differ at offset 0x%lX.\n",offset);
  108.                 }
  109.             }
  110.         }
  111.     }
  112. }
  113.  
  114. /*----------------------------*/
  115. int compare_files(Str_ptr file1,
  116.                   Str_ptr file2)
  117. {
  118. int bytes_read, fh1, fh2, error=0;
  119. Uint i, block = 0;
  120.  
  121. if ( ( fh1 = open(file1,O_RDONLY|O_BINARY) ) == -1 )
  122.     return ( -1 );
  123.  
  124. if ( ( fh2 = open(file2,O_RDONLY|O_BINARY) ) == -1 )
  125.     {
  126.     close(fh1);
  127.     return ( -2 );
  128.     }
  129.  
  130. if ( filelength(fh1) != filelength(fh2) )
  131.     error = -5;
  132. else
  133.     {
  134.     while ( !eof(fh1) )
  135.         {
  136.         if ( ( bytes_read = read(fh1,buff1,buffsize) ) == -1 )
  137.             {
  138.             error = -3;
  139.             break;
  140.             }
  141.         if ( read(fh2,buff2,bytes_read) < bytes_read )
  142.             {
  143.             error = -4;
  144.             break;
  145.             }
  146.         if ( memcmp(buff1,buff2,bytes_read) )
  147.             {
  148.             i=0;
  149.             while ( buff1[i] == buff2[i] )
  150.                 i++;
  151.             offset = block*(long)buffsize + i;
  152.             error = -6;
  153.             break;
  154.             }
  155.         block++;
  156.         }
  157.     }
  158.  
  159. close(fh1);
  160. close(fh2);
  161. return ( error );
  162. }
  163.  
  164.